package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: juliofarah
 * Date: 9/23/13
 * Time: 5:18 PM
 * To change this template use File | Settings | File Templates.
 */


public class Library {

    private ArrayList<Book> books = new ArrayList<Book>();
    private ArrayList<Movie> movies = new ArrayList<Movie>();
    private ArrayList<User> users = new ArrayList<User>();

    public Library(){
        addMovies();
        addUsers();
    }

    private void addUsers() {
        User firstUser = new User("111-1111", "julio", "name@mail.com", "Julio", "+553512341234");
        users.add(firstUser);
        setUsersLibraryNumber(firstUser);
        User secondUser = new User("111-1112", "julio", "name@mail.com", "Julio", "+553512341234");
        users.add(secondUser);
        setUsersLibraryNumber(secondUser);
    }

    public void setUsersLibraryNumber(User user){
        if (users.size() == 1)
            user.setLibraryNumber(1111111);
        else{
            int lastUserNumber = users.get(users.size() - 2).getLibraryNumber();
            user.setLibraryNumber(lastUserNumber + 1);
        }
    }

    private void addMovies(){ //TODO - this should be in a file
        movies.add(new Movie("Sholay", 1975, "Ramesh Sippy", "N/A"));
        movies.add(new Movie("The Shawshank Redemption", 1994, "Frank Darabont", "9.3"));
        movies.add(new Movie("The Godfather", 1972, "Francis Ford Coppola", "9.2"));
        movies.add(new Movie("Pulp Fiction", 1994, "Quentin Tarantino", "9"));
        movies.add(new Movie("The Good, the Bad and the Ugly", 1966, "Sergio Leone", "9"));
        movies.add(new Movie("The Dark Knight", 2008, "Christopher Nolan", "9"));
        movies.add(new Movie("12 Angry Men", 1957, "Sidney Lumet", "8.9"));
        movies.add(new Movie("Schindler's List", 1993, "Steven Spielberg", "8.9"));
        movies.add(new Movie("One Flew Over the Cuckoo's Nest", 1975, "Milos Forman", "8.8"));
        movies.add(new Movie("Seven Samurai", 1954, "Akira Kurosawa", "8.8"));
        movies.add(new Movie("Goodfellas", 1990, "Martin Scorsese", "8.8"));
        movies.add(new Movie("Inception", 2010, "Christopher Nolan", "8.8"));
        movies.add(new Movie("Star Wars: Episode IV - A New Hope", 1977, "George Lucas", "8.7"));
        movies.add(new Movie("The Lord of The Rings", 2001, "Petter Jackson", "8"));
        movies.add(new Movie("Forrest Gump", 1994, "Alan Silvestri", "7"));
        movies.add(new Movie("Dumb & Dumber", 1994, "Farrelly, Bobby and Peter Farrelly", "8"));
    }

    public void addBook(Book book){
        books.add(book);
    }

    public boolean reserveBook(Book book) {
        if (!book.isReserved()){
            book.reserve();
            return true;
        }
        return false;
    }

    public String booksToString(){
        String listOfBooks = "";
        for(Book book : books){
            listOfBooks += book.toString();
        }
        return listOfBooks;
    }

    public String moviesToString(){
        System.out.println("Movie | Year | Director | Rating");
        String listOfMovies = "";
        for (Movie movie : movies){
            listOfMovies += movie.toString();
        }
        return listOfMovies;
    }

    public User login(String username, String password){
        User user = findUser(username);
        if (user != null){
            if (user.getPassword().equals(password)){
                user.setLoggedIn(true);
                return user;
            }
        }
        return null;
    }

    public User findUser(String username) {
        username = username.replaceAll("-","");
        for(User u : users){
            if (u.getUsername().equals(username)){
                return u;
            }
        }
        return null;
    }

    public Book findBook(String title){
        for (Book book : books){
            if (book.getTitle().equals(title))
                return book;
        }
        return null;
    }

    public ArrayList<Book> getBooks() {
        return books;
    }

    public ArrayList<Movie> getMovies() {
        return movies;
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }
}
